package org.wzq.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.wzq.biz.User;

import java.util.List;

/**
 * project: spring-mvc-forum
 * ate: 14-9-17
 * author: wzq
 * description: TODO
 */
@Repository
@SuppressWarnings({"unchecked", "rawtypes"})

public class UserDao {
    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public List<User> getUserAll() {
        Session session = sessionFactory.getCurrentSession();
        List<User> list = session.createQuery("from User").list();
        return list;
    }

    @Transactional
    public void saveOrUpdate(User user) {
        sessionFactory.getCurrentSession().saveOrUpdate(user);
    }

    @Transactional
    public User isUserAndPwdMatch(String name, String pwd) {
        String hql = "from User where name=? and password=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, name);
        query.setString(1, pwd);
        return (User) query.uniqueResult();
    }

    @Transactional
    public User getUserByName(String name) {
        String hql = "from User where name=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, name);
        return (User) query.uniqueResult();
    }
}
